using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Data.Common;
using Pampa.Grid.Core.Filters.Exceptions;
using Pampa.Grid.Data;

namespace Pampa.Grid.Core.Filters
{
    public abstract class GroupFilter : Filter
    {
        protected readonly List<Filter> filters;

        protected GroupFilter() : base()
        {
            filters = new List<Filter>();
        }

        protected GroupFilter(params Filter[] innerFilters) : base()
        {
            filters = new List<Filter>();

            foreach (Filter filter in innerFilters)
            {
                this.InnerFilters.Add(filter);
            }
        }

        #region Properties

        public string Join
        {
            set { this.Source = value; }
            get { return this.Source; }
        }

        internal override string Source
        {
            set
            {
                source = value;

                foreach (Filter filter in filters)
                {
                    if (filter is SimpleFilter)
                        filter.Source = value;
                }
            }
            get
            {
                return source;
            }
        }
        
        public override bool Persistable
        {
            set 
            {
                persistable = value;

                foreach (Filter filter in filters)
                {
                    filter.Persistable = value;
                }
            }
        }

        public List<Filter> InnerFilters
        {
            get { return filters; }
        }
        
        #endregion

        #region Public Functions

        public void AddFilter(Filter filter)
        {
            if (filter is SimpleFilter)
                filter.Source = this.Source;

            filter.Parent = this;
            filters.Add(filter);
        }

        #endregion

        #region Xml

        protected override void LoadXml(XmlNode node)
        {
            XmlDocument document = node.OwnerDocument;

            if (node.Attributes["Join"] != null)
            {
                this.Join = node.Attributes["Join"].Value;
            }
            else if (node.Attributes["join"] != null)
            {
                this.Join = node.Attributes["join"].Value;
            }

            foreach (XmlNode nodeFilter in node.ChildNodes)
            {
                AddFilter(Filter.FromXml(nodeFilter));
            }
        }

        protected override void BuildNode(ref XmlNode node)
        {
            XmlDocument document = node.OwnerDocument;

            XmlAttribute attribute = document.CreateAttribute("Join");
            attribute.Value = this.Join;
            node.Attributes.Append(attribute);

            foreach (Filter filter in filters)
            {
                if (filter.Persistable)
                {
                    filter.ToXml(ref node);
                }
            }
        }

        #endregion

        #region Abstract Functions
        
        public abstract string GetFilterType();

        #endregion
    }
}
